const { Controller } = require("egg");
class BannerController extends Controller {
  async list() {
    const { service } = this;
    try {
      const result = await service.banner.list();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async create() {
    const { ctx, service } = this;
    const { title, linkUrl, bannerUrl } = ctx.request.body;
    if (!linkUrl || !bannerUrl || !title) {
      return this.ctx.helper.error("参数异常");
    }
    try {
      const result = await service.banner.create({
        title,
        linkUrl,
        bannerUrl,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async delete() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;
    try {
      const result = await service.banner.delete(id);
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async update() {
    const { ctx, service } = this;
    const { id, title, linkUrl, bannerUrl } = ctx.request.body;
    if (!id || (!title && !linkUrl && !bannerUrl)) {
      return this.ctx.helper.error("参数异常");
    }

    try {
      const result = await service.banner.update({
        id,
        title,
        linkUrl,
        bannerUrl,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

}

module.exports = BannerController;
